<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商城主页</title>
<script type="text/javascript" src="../js/jquery-3.1.0.min.js"></script>
<link rel="stylesheet" href="css/style.css" type="text/css">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript">
$(document).ready(function() {
	var TotalPage = ${TotalPage};
	var CurrentPage = ${CurrentPage};
	$("#next").click(function() {
		if (CurrentPage==TotalPage) {
			alert("当前页已经是最后一页啦");
		} else {
			CurrentPage++;
			getList;
		}
	});
	$("#previous").click(function() {
		if (CurrentPage==1) {
			alert("当前页已经是第一页啦");
		} else {
			CurrentPage--;
			getList;
		}
	});		
	$("#first").click(function() {
		CurrentPage=1;
		getList();
	});
	$("#last").click(function() {
		CurrentPage = TotalPage;
		getList();
	});
	$("#search").click(function(){
		CurrentPage=1;
		getList();
	});
	
	function getList(){
		var keyName = $("#keyName").val();
		location.href="GetBuyerGoodsListServlet?CurrentPage="+CurrentPage+"&keyName="+keyName;
	}
});
</script>
</head>
<body>
<div align="right">
	<a href="GetBuyerGoodsListServlet">商城主页</a>
	<a href="ShoppingcartListServlet">我的购物车</a>
	<a href="GetListorderServlet">我的订单</a>
	<a href="userinformation.jsp">个人信息</a>
</div>
<hr color="orange" size="5pt">
<div id="container">
	商品名称：<input type="text" id="keyName" value="${param.keyName}">
	<input type="button" id="search" value="查询"><br>
<table border="1" width="100%" bgcolor="orange">
	<tr>
		<th>商品类型</th><th>商品名称</th><th>价格</th><th>库存</th><th>店铺名称</th><th>店铺老板</th><th>联系方式<th>数量</th><th>操作</th>
	</tr>
	<c:forEach items="${goodsList}" var="goods">
		<tr>
			<td>${goods.goodsType.goodstype_name}</td><td>${goods.gName}</td><td>${goods.gPrice}</td><td>${goods.gAccount}</td><td>${goods.user.userShopName}</td><td>${goods.user.userTrueName}</td><td>${goods.user.userPhone}</td>					
			<td><input type="text" name="goods_num"  id="t${goods.gid}" onmouseout="checknum(this)" ></td>				
			<td>
				<input type="button" value="加入购物车" disabled="disabled" onclick="add(${goods.gid},'${goods.goodsType.goodstype_name}','${goods.gName}',${goods.gPrice},${goods.gAccount},'${goods.user.userShopName}','${goods.user.userTrueName}','${goods.user.userPhone}','${goods.user.userId}',this)">
				<input type="button" value="下单" disabled="disabled"  onclick="buy(${goods.gid},'${goods.goodsType.goodstype_name}','${goods.gName}',${goods.gPrice},${goods.gAccount},'${goods.user.userShopName}','${goods.user.userTrueName}','${goods.user.userPhone}','${goods.user.userId}',this)">
			</td>
		</tr>
	</c:forEach>
</table>
<input type="button" value=" 第一页" id="first"> 
<input type="button" value=" 上一页" id="previous"> 
<input type="button"value="下一页" id="next"> 
<input type="button" value="最后页" id="last"> 
${CurrentPage}/${TotalPage}
</div>
<script type="text/javascript">
function checknum(obj){
	var goods_num=$(obj).val();
	if(goods_num>0){
		$(obj).parent().next().children().removeAttr("disabled");
	}else{
		$(obj).parent().next().children().attr("disabled","disabled");
	}
}
function buy(gid,goodstype_name,gName,gPrice,gAccount,userShopName,userTrueName,userPhone,userId,obj){
	var goods_num=$("#t"+gid).val();
	alert(goods_num);
	if(goods_num<=gAccount){
		location.href ="AddOrderServlet?gid="+gid+"&goodstype_name="+goodstype_name+"&gName="+gName+"&gPrice="+gPrice+"&gAccount="+gAccount+"&UserShopName="+userShopName+"&UserShopName="+userTrueName+"&UserPhone="+userPhone+"&goods_num="+goods_num+"&UserId="+userId+"&order_state=1"+"&CurrentPage="+${CurrentPage};
	}else{
		alert("库存不足,库存还剩"+gAccount);
	}
}
function add(gid,goodstype_name,gName,gPrice,gAccount,userShopName,userTrueName,userPhone,userId,obj){
	var goods_num=$("#t"+gid).val();
	if(goods_num<=gAccount){
		location.href ="AddShoppingCartServlet?gid="+gid+"&goodstype_name="+goodstype_name+"&gName="+gName+"&gPrice="+gPrice+"&gAccount="+gAccount+"&userShopName="+userShopName+"&userShopName="+userTrueName+"&userPhone="+userPhone+"&goods_num="+goods_num+"&userId="+userId+"&order_state=0"+"&CurrentPage="+${CurrentPage};	
	}else{
		alert("库存不足,库存还剩!!库存仅剩最后"+gAccount);
	}
}
</script>
</body>
</html>